package com.avic.rong;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.utils.SharedPreferencesUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseLanFragment extends Fragment {
    /**
     * Fragment的View加载完毕的标记
     */
    protected boolean isViewInitiated;
    /**
     * Fragment对用户可见的标记
     */
    protected boolean isVisibleToUser;
    /**
     * 是否懒加载
     */
    protected boolean isDataInitiated;


    protected View fragmentView;
    protected Activity context;
    protected ViewGroup containerView;
    protected LayoutInflater mInflater;

    /**
     * 绑定,解绑butterknife
     */
    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (fragmentView == null) {
            fragmentView = inflater.inflate(getContentViewId(), container, false);
            unbinder = ButterKnife.bind(this, fragmentView);

            context = getActivity();
            mInflater = LayoutInflater.from(context);

            initView();
            addListener();
        }
        unbinder = ButterKnife.bind(this, fragmentView);

        isViewInitiated=true;
        return fragmentView;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        if (mInflater == null) {
            mInflater = LayoutInflater.from(context);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        prepareFetchData();
        lazyLoad();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    /***
     * 初始化view
     */

    protected abstract void initView();

    /***
     * 注册监听器
     */
    protected abstract void addListener();

    private SharedPreferencesUtil sharedPreferencesUtil;

    protected SharedPreferencesUtil getSharedPreferencesUtil() {
        if (sharedPreferencesUtil == null) {
            sharedPreferencesUtil = SharedPreferencesUtil.getInstance(context);
        }
        return sharedPreferencesUtil;
    }


    /***
     * 获取布局
     *
     * @return
     */
    protected abstract int getContentViewId();




    protected <T extends View> T $(int id) {
        return (T) fragmentView.findViewById(id);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();

    }
    /**
     * 第三步:在lazyLoad()方法中进行双重标记判断,通过后即可进行数据加载
     * 第一种方法
     * 调用懒加载，getUserVisibleHint()会返回是否可见状态
     * 这是fragment实现懒加载的关键,只有fragment 可见才会调用onLazyLoad() 加载数据
     */
    private void lazyLoad() {
        if (getUserVisibleHint() && isViewInitiated && !isDataInitiated) {
            fetchData();
            isDataInitiated = true;
        }
    }

    /**
     * 第二种方法
     * 调用懒加载
     */
    public void prepareFetchData() {
        prepareFetchData(false);
    }
    /**
     * 第三步:在lazyLoad()方法中进行双重标记判断,通过后即可进行数据加载
     */
    public void prepareFetchData(boolean forceUpdate) {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            fetchData();
            isDataInitiated = true;
        }
    }
    public abstract void fetchData();
}
